package com.mingmen.supplier.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mingmen.supplier.R;
import com.mingmen.supplier.model.CommodityAllModel;
import com.mingmen.supplier.model.WeiHuModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LJZ
 * on 2019-11-19
 * Describe:
 */
public class WeiHuLeftAllAdapter extends RecyclerView.Adapter<WeiHuLeftAllAdapter.ViewHolder> {

    private Context mContext;
    private List<WeiHuModel.DataBean> mListData = new ArrayList<>();
    private int checkedPosition;


    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public WeiHuLeftAllAdapter(Context mContext, List<WeiHuModel.DataBean> mListData) {
        //  mListData = new ArrayList<>();
        this.mContext = mContext;
        this.mListData = mListData;
    }

    public void update(List<WeiHuModel.DataBean> list) {

        this.mListData = list;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView typeTv;
        LinearLayout root_layout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            typeTv = (TextView) itemView.findViewById(R.id.alive_type_tv);
            root_layout = (LinearLayout) itemView.findViewById(R.id.root_layout);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (mContext == null) {
            mContext = viewGroup.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_commdity_all, viewGroup, false);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
//        Set<Integer> positionSet = MultipleChoiceActivity.positionSet;
//        Set<Integer> positionSet = CommodityShopActivity.positionSet;
        //检查set里是否包含position，包含则显示选中的背景色，不包含则反之
//        Log.d("POSITIONSET", positionSet.toString());

//        大于50绿色 小于红色
        if (position == checkedPosition) {
            holder.root_layout.setBackgroundColor(Color.parseColor("#58B968"));
            holder.typeTv.setTextColor(Color.parseColor("#ffffff"));
        } else {
            holder.root_layout.setBackgroundColor(Color.parseColor("#F1F1F1"));
            holder.typeTv.setTextColor(Color.parseColor("#282828"));
        }

        WeiHuModel.DataBean bean = mListData.get(position);
        holder.typeTv.setText(bean.getCategoryName());
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.OnItemClick(view, pos, mListData);

                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mListData != null ? mListData.size() : 0;
    }

    public interface OnItemClickListener {

        //单点监听
        void OnItemClick(View view, int position, List<WeiHuModel.DataBean> mListData);


    }

}
